package self.sn.chat.ui;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;

public class MyFrame extends JFrame {

    public enum Op {
        MSG,SEND_MSG,ONLINE
    }

    private JTextArea msgArea;
    private JTextArea sendMsgArea;
    private JTextArea onlineArea;
    private JButton sendBtn;
    private final HashMap<Op, JTextArea> AREAS = new HashMap<>();

    public void init() {
        this.msgArea = new JTextArea();
        this.msgArea.setEditable(false);
        this.sendMsgArea = new JTextArea();
        this.onlineArea = new JTextArea();
        this.onlineArea.setEditable(false);
        this.sendBtn = new JButton("发送");

        AREAS.put(Op.MSG, this.msgArea);
        AREAS.put(Op.SEND_MSG, this.sendMsgArea);
        AREAS.put(Op.ONLINE, this.onlineArea);
    }

    public MyFrame(){
        init();

        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        JPanel msgPanel = new JPanel(null);
        msgPanel.setBounds(5, 5, 400, 500);
        JScrollPane scrollPane = new JScrollPane(msgArea);
        scrollPane.setBounds(5, 5, 390, 300);
        msgPanel.add(scrollPane);
        sendMsgArea.setBounds(5, 310, 390, 100);
        msgPanel.add(sendMsgArea);
        sendBtn.setBounds(310,415, 80, 30);
        msgPanel.add(sendBtn);
        contentPane.add(msgPanel);

        onlineArea.setBounds(405, 10, 170, 440);
        contentPane.add(onlineArea);

        setTitle("聊天室v1   ©songbiao");
        setSize(600, 500);
        setLocation(300,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    public void paintTxt(Op op, String text) {
        JTextArea textArea = AREAS.get(op);
        if (op.equals(Op.ONLINE)) {
            textArea.setText(text);
        } else {
            textArea.insert(text, textArea.getText().length());
        }
    }

    public JTextArea getMsgArea() {
        return msgArea;
    }

    public JTextArea getSendMsgArea() {
        return sendMsgArea;
    }

    public JTextArea getOnlineArea() {
        return onlineArea;
    }

    public JButton getSendBtn() {
        return sendBtn;
    }
}
